<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.List"%>
<%@ page import="net.contrend.system.form.MenuForm"%>
<%@page import="net.contrend.util.StringUtils"%>
<%
    pageContext.setAttribute("PAGETITLE", "主页");
    
    List<MenuForm> menus = (List<MenuForm>) session.getAttribute("menus");
%>
<%!
    private String buildTree(List<MenuForm> menus, String div, String menuAction) {
        StringBuilder sb = new StringBuilder();
        this.appendln(sb, "var tree = new JsTree(\"" + div + "\");");
        this.appendln(sb, "tree.iconPath = ctx + \"/img/jstree/\";");
        this.appendln(sb, "var root = new JsNode(\"CWMS\");");
        //String rootText = "<a href=\\\"javascript:void(0)\\\">CWMS</a>";
        //this.appendln(sb, "root.text = \"" + rootText + "\";");
        this.appendln(sb, "tree.setRoot(root);");
        for (MenuForm menu : menus) {
            String text = "<a href=\\\"javascript:" + menuAction + "({url: ctx + '" + menu.getMenuUrl() + "'})\\\">" + menu.getMenuName() + "</a>";
            
            String vname = "menu" + menu.getMenuId().replaceAll("-", "");
            String pname = "menu" + menu.getParentId().replaceAll("-", "");
	        this.appendln(sb, "var " + vname + " = new JsNode(\"" + vname + "\");");
	        this.appendln(sb, vname + ".text = \"" + text + "\";");
	        if (StringUtils.isNotBlank(menu.getParentId())) {
	            this.appendln(sb, pname + ".add(" + vname + ");");
	        } else {
		        this.appendln(sb, "root.add(" + vname + ");");
	        }
        }
        return sb.toString();
    }

    private void appendln(StringBuilder sb, String s) {
        sb.append(s).append("\n");
    }
%>
<%@ include file="/pages/include/header.jsp" %>

<style>
<!--
html, body {overflow: hidden;}
-->
</style>

<script type="text/javascript">
<!--
function cmd(command, params) {
    switch (command) {
	    case "clickMenu":
	        var url = ctx + params.url;
	        $E("main").src = url;
	        break;
    }
}

var clickMenu = function (params) {
    $E("main").src = params.url;
};

$(document).ready(function () {
<%=this.buildTree(menus, "div_menu", "clickMenu")%>
    $.get(ctx + "/pages/system/menuEdit.jsp", function (returnString) {
        //alert(returnString);
        //$("#div_main").html(returnString);
    });
});
//-->
</script>

<body>

    <div class="index.title">
        <h1>CWMS</h1>
        <span style="float: right;">
            <a href="${ctx}/action/login.do?method=logout">注销</a>
        </span>
    </div>

    <div class="index_left" id="div_menu">
        
    </div>
    
    <div class="index_main" id="div_main">
        <iframe id="main" scrolling="auto" frameborder="0" width="100%" height="100%"></iframe>
    </div>

</body>

<script type="text/javascript">
<!--

//-->
</script>

<%@ include file="/pages/include/footer.jsp" %>
